package edu.mtu.citizenscience.cs.notepad.activity.noteslist;

import java.util.List;

import com.androidquery.AQuery;

import edu.mtu.citizenscience.cs.notepad.activity.R;
import edu.mtu.citizenscience.cs.notepad.model.ModelNote;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Note List Adapter adapts note models into note list
 */
public class NoteListAdapter extends ArrayAdapter<ModelNote> {
	// android-query library
	private AQuery aq;

	/**
	 * Create the Note List Adapter
	 * 
	 * @param aq - AQuery - reference to housing activities android query instance
	 * @param notes - List<ModelNote> - list of notes
	 * @return void
	 */
	public NoteListAdapter(AQuery aq, List<ModelNote> notes)
	{
		// create array adapter with list note layout and note models
		super(aq.getContext(), R.layout.list_item_note, notes);
		
		// set the android query to instance from containing activity
		this.aq = aq;
	}

	/**
	 * Get the view for a Note List note
	 * 
	 * @param int - position - the views position in the list
	 * @param convertView - View - the view for the list item
	 * @param parent - ViewGroup - containing list view
	 * @return View - the populated view for the list item
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{
		// check if the view has not been inflated before
		if (convertView == null) 
	    {
			// Inflate the new view
	    	convertView = this.aq.inflate(convertView, R.layout.list_item_note, parent);
	    }
		// get the model note for this list position
		ModelNote note = this.getItem(position);
		
		// get the note title text view
		TextView title = (TextView) convertView.findViewById(R.id.noteTitle);
		// get the note camera image view
		ImageView camera = (ImageView) convertView.findViewById(R.id.imageCamera);
		// get the note location image view
		ImageView location = (ImageView) convertView.findViewById(R.id.imageLocation);
		// get the note recording image view
		ImageView recording = (ImageView) convertView.findViewById(R.id.imageRecording);
		
		// get the note title to the title from the note model
		title.setText(note.getTitle());
		
		// check if the note has no images
		if (note.getImages().size() == 0)
		{
			// don't show the camera icon as don't have any images
			camera.setVisibility(ImageView.INVISIBLE);
		}
		else
		{
			// show the camera icon as have some images
			camera.setVisibility(ImageView.VISIBLE);
		}
		
		// check if the note does not have a location
		if (note.getLocation().getId() == -1)
		{
			// don't show the location icon as don't have a location
			location.setVisibility(ImageView.INVISIBLE);
		}
		else
		{
			// show the location icon as have a location
			location.setVisibility(ImageView.VISIBLE);
		}
		
		// check if the note does not have a recording
		if (note.getRecording().getId() == -1)
		{
			// don't show the recording icon as don't have a recording
			recording.setVisibility(ImageView.INVISIBLE);
		}
		else
		{
			// show the recording icon as have a recording
			recording.setVisibility(ImageView.VISIBLE);
		}
		
		// return the list item view
		return convertView;
	}
}
